import store from "@/store";
import router from "@/router";

router.beforeEach(async (to, from, next) => {
  let token = store.state.user.token;
  let nickName = store.state.user.nickName;
  let whiteList = ["/trade", "/pay", "/paysuccess", "/center", "/center/myorder", "/center/teamorder"];

  if (token) {
    if (to.path == "/login" && to.path == "/register") {
      next("/home");
    } else {
      if (nickName) {
        next();
      } else {
        try {
          await store.dispatch("user/getUserValueFun");
          next();
        } catch (error) {
          store.dispatch("user/logOutFun");
          next("/login");
        }
      }
    }
  } else {
    let toPath = to.path;
    if (whiteList.indexOf(toPath) != -1) {
      next("/login?redirect=" + toPath);
    } else {
      next();
    }
  }
});
